#version 330 core

uniform transform {
	mat4 ModelViewMatrix;
	mat4 ProjectionMatrix;
	float TimeFactor;
} Transform;

uniform vec3 SkyLightDir;
uniform vec3 FogColor;

uniform samplerBuffer InstanceData;

in vec3 VertexPosition;
in vec3 VertexNormal;
in vec2 VertexTexCoord;
in float DeformRatio;

out vec4 FogParam;
out vec3 InterpNormal;
out vec2 InterpTexCoord;
out vec3 InterpViewDir;
out vec3 InterpLightDir;

void main(void) {

	/* fetch instance position from texture buffer and add the local vertex coordinate to it */
	vec4 ObjectSpacePosition = texelFetch(InstanceData, gl_InstanceID) + vec4(VertexPosition, 1.0);
	
	/* hackish deformation to produce wind-like effect */
	ObjectSpacePosition.x += DeformRatio * (cos( Transform.TimeFactor )+1.0) * 2.0;
	ObjectSpacePosition.y += DeformRatio * (sin( Transform.TimeFactor )+1.0) * 0.5;
	
	/* transform into view space */
	vec4 ViewSpacePosition = Transform.ModelViewMatrix * ObjectSpacePosition;
	
	/* calculate fog related information */
	FogParam.xyz = FogColor;
	FogParam.w = exp(length(ViewSpacePosition.xyz) / 50.0) / 2.6;
    
	/* transform into clip space */
	gl_Position = Transform.ProjectionMatrix * ViewSpacePosition;
	
	/* pass through other attributes */
	InterpNormal = VertexNormal;
	InterpTexCoord = VertexTexCoord;	
	InterpViewDir = vec3( 0.0, 0.0, 1.0 );
	InterpLightDir = SkyLightDir; 

}
